﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Calorie
{
    public class Calorie:ICalorie
    {
        public void InsertCalorieCount(int calories, int userId, string description, string metadata, DateTime clientDateTime, ref Model.NutriLeafDBEntities context)
        {
            Model.CalorieDiary calorie = new Model.CalorieDiary();
            calorie.CalorieCount = calories;
            calorie.UserID = userId;
            calorie.Description = description;
            calorie.Metadata = metadata;
            calorie.ClientDateTimeStamp = clientDateTime;
            calorie.DateTimeStamp = DateTime.Now;
            context.CalorieDiaries.AddObject(calorie);
            context.SaveChanges();
           
        }

        public void InsertCalorieCount(Model.CalorieDiary calorie, ref Model.NutriLeafDBEntities context)
        {
            context.CalorieDiaries.AddObject(calorie);
            context.SaveChanges();

         
        }

        public void UpdateCalorieCount(Model.CalorieDiary calorie, ref Model.NutriLeafDBEntities context)
        {
            if (context.CalorieDiaries.Where(p => p.CalorieDiaryID == calorie.CalorieDiaryID).Count() != 0)
            {
                Model.CalorieDiary temp = context.CalorieDiaries.Where(p => p.CalorieDiaryID == calorie.CalorieDiaryID).First();
                context.CalorieDiaries.DeleteObject(temp);
                context.SaveChanges();
                context.AddToCalorieDiaries(calorie);
                context.SaveChanges();
              

            }
        }

        public int GetCaloriesForADay(int userId, DateTime clientDatetime, ref Model.NutriLeafDBEntities context)
        {

            if (context.CalorieDiaries.Where(p => p.ClientDateTimeStamp.Value == clientDatetime && p.UserID == userId).Count() != 0)
            {
                Model.CalorieDiary calor = context.CalorieDiaries.Where(p => p.ClientDateTimeStamp.Value == clientDatetime && p.UserID == userId).First();
                return calor.CalorieCount ;
            }
            else {
                return -1;

            }
        }

        public void RemoveRecentCalorieUpdate(int userId, ref Model.NutriLeafDBEntities context)
        {
            if (context.CalorieDiaries.Where(p => p.UserID == userId).Count() != 0)
            {
                Model.CalorieDiary[] cal = context.CalorieDiaries.Where(p => p.UserID == userId).ToArray();
                  foreach(Model.CalorieDiary calor in cal){
                  context.CalorieDiaries.DeleteObject(calor);
                  
                  }
                context.SaveChanges();
            }
        }
        public void RemoveCalorieDiaryEntry(int calorieId, ref Model.NutriLeafDBEntities context)
        {

            if (context.CalorieDiaries.Where(p => p.CalorieDiaryID == calorieId).Count() != 0) {
                Model.CalorieDiary ca = context.CalorieDiaries.Where(p => p.CalorieDiaryID == calorieId).First();
                context.DeleteObject(ca);
                context.SaveChanges();
            }
        }

        public List<Model.CalorieDiary> GetCalorieCountForADay(int userId, DateTime clientDatetime, ref Model.NutriLeafDBEntities context)
        {

            return context.CalorieDiaries.Where(p => p.UserID == userId && p.ClientDateTimeStamp.Value ==clientDatetime).ToList();
          
            
        }

        public Model.CalorieDiary GetCalorie(int calorieId, ref Model.NutriLeafDBEntities context)
        {
            if (context.CalorieDiaries.Where(p => p.CalorieDiaryID == calorieId).Count() != 0)
            {
                return context.CalorieDiaries.Where(p => p.CalorieDiaryID == calorieId).First();
            }
            else
                return null;
        }

        public List<Model.CalorieDiary> GetCalorieCount(int userId, DateTime clientDatetimeStart, DateTime clientDateTimeEnd, ref Model.NutriLeafDBEntities context)
        {
            return context.CalorieDiaries.Where(p => p.ClientDateTimeStamp > clientDatetimeStart && p.ClientDateTimeStamp < clientDateTimeEnd).ToList();
            
        }

        public int GetTodayCalorieIntakeDifference(int userId, DateTime clientDateTime, ref Model.NutriLeafDBEntities context)
        {
            return 0;
            //where is other diffrence value.
        }
    }
}